package com.gw.quotes.activity.fragment;

import java.util.ArrayList;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gw.quotes.MyApplication;
import com.gw.quotes.R;
import com.gw.quotes.activity.LoginActivity;
import com.gw.quotes.activity.MyOrderActivity;
import com.gw.quotes.activity.SetInfoActivity;
import com.gw.quotes.entity.business.OrderListEntity.OrderItem;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.OrderlistQueryServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.gw.quotes.util.DialogUtils;
import com.gw.quotes.util.DialogUtils.DialogCallBack;

public class OrderFragment extends BaseMainFragment {

	private View contextView = null;
	private ArrayList<OrderItem> orderlist;

	@ViewInject(id = R.id.set_info_img, click = "setInfo")
	private ImageView set_info_img;
	@ViewInject(id = R.id.head_img)
	private ImageView head_img;
	@ViewInject(id = R.id.user_name)
	private TextView user_name;
	@ViewInject(id = R.id.my_order_text, click = "myOrder")
	private TextView myOrder_text;
	@ViewInject(id = R.id.rel1, click = "rel1")
	private RelativeLayout rel1;
	@ViewInject(id = R.id.rel2, click = "rel2")
	private RelativeLayout rel2;

	// @ViewInject(id = R.id.rel3, click = "rel3")
	// private RelativeLayout rel3;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (contextView == null) {
			contextView = inflater.inflate(R.layout.fragment_activity_order,
					null);
		}
		ViewGroup parent = (ViewGroup) contextView.getParent();
		if (parent != null) {
			parent.removeView(contextView);
		}
		FinalActivity.initInjectedView(this, contextView);
		initView();
		return contextView;
	}

	@Override
	public void onResume() {
		super.onResume();

		orderlist = MyApplication.getOrderlist();

		if (getLoginUser() == null) {
			DialogUtils.alertDialog("您尚未登录，请先登录！", mActivity,
					new DialogCallBack() {
						@Override
						public void onNegativeButton(Object o) {
							Intent intent = new Intent(mActivity,
									LoginActivity.class);
							startActivity(intent);
						}
					});
		} else {
			user_name.setText(getLoginUser().getLoginname());
			if (orderlist == null) {
				// 启动加载数据
				requestList();
			} else {
//				for (int i = 0; i < orderlist.size(); i++) {
//					Log.i("xd", "status " + orderlist.get(i).getStatus());
//				}
			}
		}
	}

	private void requestList() {
		dialog.show();
		BaseServiceInter httpService = new OrderlistQueryServiceImpl();
		Request request = new Request();
		request.setLoginname(getLoginUser().getLoginname());
		request.setTicket(getLoginUser().getTicket());
		request.setJsonconditions();
		Response response = httpService.handler(request,
				new HttpCallBack<Response<ArrayList<OrderItem>>>() {
					@Override
					public void onFinished(
							Response<ArrayList<OrderItem>> response) {
						if (response != null
								&& response.getResult() == response.RESULT_SUCCESS) {
							orderlist = response.getObj();
							MyApplication.instance().setOrderlist(orderlist);
							// aCache.put(
							// "orderlist_"
							// + AppInfo.LOGIN_USER.getLoginname(),
							// orderlist);
						} else {
							if (response != null) {
								CommonTools.showShortToast(context,
										response.getMsg());
							} else {
								CommonTools.showShortToast(context,
										"获取数据失败，请检查网络");
							}
						}
						dialog.dismiss();
					}
				});
	}

	private void initView() {
		// orderText.setText("order");
	}

	// OnClick
	// 信息设置
	public void setInfo(View v) {
		Intent intent = new Intent(contextView.getContext(),
				SetInfoActivity.class);
		startActivity(intent);
	}

	private void checkLogin(int index) {
		if (getLoginUser() == null) {
			DialogUtils.alertDialog("您尚未登录，请先登录！", mActivity,
					new DialogCallBack() {
						@Override
						public void onNegativeButton(Object o) {
							Intent intent = new Intent(mActivity,
									LoginActivity.class);
							startActivity(intent);
						}
					});
		} else {
			if ((orderlist = MyApplication.getOrderlist()) == null) {
				// 启动加载数据
				requestList();
				return;
			}
			Intent intent = new Intent(contextView.getContext(),
					MyOrderActivity.class);
			intent.putExtra("index", index);
			intent.putExtra("orderlist", orderlist);
			startActivity(intent);
		}
	}

	public void myOrder(View v) {
		checkLogin(-1);
	}

	public void rel1(View v) {
		checkLogin(0);
	}

	public void rel2(View v) {
		checkLogin(1);
	}

	public void rel3(View v) {
		checkLogin(3);
	}

}
